﻿package core.scene {
	import flash.display.MovieClip;
	import flash.text.TextField;
	import core.dialog.DialogLogin;
	import core.events.LoginEvent;
	import base.BaseMc;
	import core.dialog.DialogMessage;
	import core.User;
	import core.events.CmdEvent;
	import core.net.socket.cmd.GetBarCodeCmd;
	import core.events.BgSoundEvent;
	import core.display.LoginMenu;
	import utils.UtilScreen;
	import core.dialog.DialogLoading;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	
	public class SceneLogin extends BaseMc{
		
		
		private var dlgLogin:DialogLogin;
		private var back:Boolean = false;
		private var menuRight:LoginMenu;
		private var menuLeft:LoginMenu;
		private var loading:DialogLoading = new DialogLoading();
		private var reloginTimer:Timer;
		private var loginCount:int = 0;
		
		public function SceneLogin(back:Boolean = false) {
			this.back = back;
		}
		
		protected override function onInit():void {
			trace("SceneLogin onInit");
			super.onInit();
			this.initMenuButton();
			this.dispatchEvent(new BgSoundEvent(BgSoundEvent.START));
			this.dlgLogin = new DialogLogin(this.back);
			this.dlgLogin.addEventListener(LoginEvent.LOGIN,onLogin);
			this.dlgLogin.addEventListener(LoginEvent.SUCCESS,onLoginSuccess);
			this.dlgLogin.addEventListener(LoginEvent.FAILED,onLoginFailed);
			this.addChild(dlgLogin);
			reloginTimer = new Timer(5000);
		}
		
		
		//初始化按钮元件
		private function initMenuButton():void {
			
			//var screenHeight = UtilScreen.getScreenHeight();
			//var screenWidth = UtilScreen.getScreenWidth();
			var screenHeight = this.stage.stageHeight;
			var screenWidth = this.stage.stageWidth;
			trace("screenHeight:" + screenHeight);
			trace("screenWidth:"+screenWidth);
			
			//右边位置
			this.menuRight = new LoginMenu();
			this.menuRight.scaleX = this.menuRight.scaleX * 0.7;
			this.menuRight.scaleY = this.menuRight.scaleY * 0.7;
			this.menuRight.y = screenHeight - 120;
			this.menuRight.x = screenWidth - 120;
			this.addChild(this.menuRight);
			
			//左边位置
			this.menuLeft = new LoginMenu();
			this.menuLeft.scaleX = this.menuLeft.scaleX * 0.7;
			this.menuLeft.scaleY = this.menuLeft.scaleY * 0.7;
			this.menuLeft.y = screenHeight - 120;
			this.menuLeft.x = 50;
			this.addChild(this.menuLeft);
			
		}
		
		protected override function onRemove():void {
			super.onRemove();
			this.dlgLogin.removeEventListener(LoginEvent.LOGIN,onLogin);
			this.dlgLogin.removeEventListener(LoginEvent.SUCCESS,onLoginSuccess);
			this.dlgLogin.removeEventListener(LoginEvent.FAILED,onLoginFailed);
		}
		
		
		private function onLogin(e:LoginEvent):void {
			loading.Title = "正在登录";
			loading.Message = "请稍等";
			this.addChild(loading);
			this.addChild(this.menuLeft);
			this.addChild(this.menuRight);
			
		}
		
		private function onLoginSuccess(e:LoginEvent):void {
			trace("登录成功");
			//User.id = e.data.user_id;
			//this.sendCmd(GetBarCodeCmd.make());
			//User.deviceAddress = e.data.address;
			//显示大厅
			var sceneHall:SceneHall = new SceneHall();
			this.parent.addChild(sceneHall);
			// 移除自身
			this.remove();
		}
		
		private function onLoginFailed(e:LoginEvent):void {
			//弹出用户名或密码错误对话框
			trace("登录失败");
			//var dlg:DialogMessage = new DialogMessage("登录失败:"+e.data.error,true);
			//this.addChild(dlg);
			if(this.loginCount == 2){
				this.loading.remove();
				var dlg:DialogMessage = new DialogMessage("多次登录失败:"+e.data.error,true);
				this.addChild(dlg);
			}
			else{
				loading.Title = "登录失败";
				loading.Message = e.data.error + "(5秒后重新登录)";
				this.addChild(loading);
				reloginTimer.addEventListener(TimerEvent.TIMER,onTimer);
				reloginTimer.start();
			}
			
		}
		
		private function onTimer(e:TimerEvent):void {
			reloginTimer.stop();
			this.loginCount++;
			this.dlgLogin.login();
		}
		
		protected override function onCommandResponse(e:CmdEvent):void{
			super.onCommandResponse(e);
			
			var data:Object = e.data;
			if(data.cmd == GetBarCodeCmd.COMMAND_ID){
				if(data.error == ""){
					//User.deviceAddress = e.data.data.address;
					//显示大厅
					//var sceneHall:SceneHall = new SceneHall();
					//this.parent.addChild(sceneHall);
					// 移除自身
					//this.remove();
				}else{
					var dlg:DialogMessage = new DialogMessage("登录失败:"+e.data.error,true);
					this.addChild(dlg);
				}
			}
			
			
			
			
			
			
			
		}
	}
	
}
